//
//  FindViewController.m
//  KEREN
//
//  Created by MW on 2023/3/10.
//

#import "FindViewController.h"
#import "KRRoundView.h"
#import "KRFindListViewController.h"
#import "KRFindCarouselModel.h"
@interface FindViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIView *currentLineView;
@property (nonatomic, strong) NSMutableArray *btnArrM;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) KRFindListViewController *csVC;
@property (nonatomic, strong) KRFindListViewController *tlVC;
@property (nonatomic, strong) KRFindListViewController *crVC;
@property (nonatomic, strong) NSMutableArray *imgsArrM;
@property (nonatomic, strong) KRRoundView *roundView;

@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainBack_image"]];
    backImage.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    [self.view addSubview:[self navBottomViewTag:1]];
  //  [self tableView];
    _roundView = [[KRRoundView alloc] initWithFrame:CGRectMake(0, STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_WIDTH/ 16 * 9) imgArray:@[]];
    [self.view addSubview:_roundView];

    [self.view addSubview:self.headView];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, DEVICE_WIDTH/ 16 * 9 + 58 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT -  DEVICE_WIDTH/ 16 * 9 - 58 - 70 - STATUSBAR_HEIGHT)];
    [self.view addSubview:_scrollView];
    _csVC = [[KRFindListViewController alloc] init];
    [_csVC getFindVehicleSkillList:0];
    //[_csVC requsetDeviceInfoList];
    _tlVC = [[KRFindListViewController alloc] init];
    _crVC = [[KRFindListViewController alloc] init];
    [self addChildViewController:_csVC];
    [self addChildViewController:_tlVC];
    [self addChildViewController:_crVC];
    
    _csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT -  DEVICE_WIDTH/ 16 * 9 - 58 - 70 - STATUSBAR_HEIGHT);
    _tlVC.view.frame = CGRectMake(DEVICE_WIDTH, 0, DEVICE_WIDTH, DEVICE_HEIGHT -  DEVICE_WIDTH/ 16 * 9 - 58 - 70 - STATUSBAR_HEIGHT);
    _crVC.view.frame = CGRectMake(DEVICE_WIDTH * 2, 0, DEVICE_WIDTH, DEVICE_HEIGHT -  DEVICE_WIDTH/ 16 * 9 - 58 - 70 -STATUSBAR_HEIGHT);
    
    [_scrollView addSubview:_csVC.view];
    [_scrollView addSubview:_tlVC.view];
    [_scrollView addSubview:_crVC.view];
   // _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(DEVICE_WIDTH * 3, 0);
    _scrollView.delegate = self;

    [self getFindpromotionalImageList];
    
}
- (void)getFindpromotionalImageList{
    WEAKSELF
    [NetworkingTool POST:Find_promotionalImage_list parameters:@{@"indx":@(1),@"size":@(20)} isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        
        if(response[@"data"][@"list"] && [response[@"data"][@"list"] isKindOfClass:[NSArray class]]){
            NSArray *dataArr = response[@"data"][@"list"];
            [self.imgsArrM  removeAllObjects];
            for (int i = 0; i < dataArr.count; i++) {
                KRFindCarouselModel *model = [KRFindCarouselModel initWithDictionary:dataArr[i]];
                [weakSelf.imgsArrM addObject:model];
            }
            if(dataArr.count == 0){
                KRFindCarouselModel *model = [[KRFindCarouselModel alloc] init];
                model.image_url = @"";
                [weakSelf.imgsArrM addObject:model];
            }
            [weakSelf.roundView setImageView:weakSelf.imgsArrM];

        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
}

- (void)btnClick:(UIButton *)sender{
    [self updateTopView:sender.tag];
    [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * sender.tag, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x = scrollView.contentOffset.x + DEVICE_WIDTH /2;
    int index = x / DEVICE_WIDTH ;
    [self updateTopView:index];
}

- (void)updateTopView:(NSInteger)tag{
    for (UIButton *btn in _btnArrM) {
        if(btn.tag == tag){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Whire_Color forState:UIControlStateNormal];
            [_currentLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(18);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(0);
            }];
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_DDDDDD forState:UIControlStateNormal];
        }
    }
    if(tag == 0){
        [_csVC getFindVehicleSkillList:0];
    }else if (tag == 1){
        [_tlVC getFindVehicleSkillList:1];
    }else{
        [_crVC getFindVehicleSkillList:2];
    }
}

- (UIView *)headView{
    if(!_headView){
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, DEVICE_WIDTH/ 16 * 9 + 10 + STATUSBAR_HEIGHT, DEVICE_WIDTH, 48)];
        NSArray *titleArr = @[@"全部",@"用车技巧",@"智能知识"];
        _headView.backgroundColor = [UIColor clearColor];
        float leftWidth = 24;
        [self.btnArrM removeAllObjects];
        for (int i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [_headView addSubview:button];
            [button setTitle:titleArr[i] forState:UIControlStateNormal];
            float titleWidth = [[NSStringManager shareInstance] measureSinglelineStringWidth:titleArr[i] andFont:BFont(20)];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(leftWidth);
                make.width.offset(titleWidth + 24);
                make.height.offset(28);
                make.top.offset(12);
            }];
            if(i == 0){
                button.titleLabel.font = BFont(20);
                [button setTitleColor:Whire_Color forState:UIControlStateNormal];
                _currentLineView = [UIView setLineViewWithframe:CGRectZero forinView:_headView];
                [_currentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(button);
                    make.height.offset(2);
                    make.width.offset(18);
                    make.bottom.offset(0);
                }];
            }else{
                button.titleLabel.font = Font_Medium(16);
                [button setTitleColor:Color_DDDDDD forState:UIControlStateNormal];
            }
            leftWidth = leftWidth + titleWidth + 24;
            button.tag = i;
            [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArrM addObject:button];
        }
    }
    return _headView;
}

- (NSMutableArray *)btnArrM{
    if(!_btnArrM){
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}

- (NSMutableArray *)imgsArrM{
    if(!_imgsArrM){
        _imgsArrM = [NSMutableArray array];
    }
    return _imgsArrM;
}

@end
